/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import Params.ChatMessage;
import Params.ServerSocketItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;

/**
 *
 * @author ANH PHONG
 */
public class ChatForm extends javax.swing.JInternalFrame implements ActionListener {

    /**
     * Creates new form ChatForm
     */
    Socket _sockhientai = null;
    public ChatForm(String strPort, Socket client) {
        initComponents();
        this.txtInputText.addActionListener(this);
        //initServer(strPort);
        _sockhientai = client;
        int temp = (client.getPort());
        this.title = "SERVER - CLIENT " + temp + "";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        btnSend = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        txtInputText = new javax.swing.JTextField();
        listChat = new java.awt.List();

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/exit.png"))); // NOI18N
        jButton2.setText("Close");

        btnSend.setText("Send");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/savefile.png"))); // NOI18N
        jButton4.setText("Save file");

        jLabel1.setBackground(new java.awt.Color(0, 204, 0));
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setText(" INPUT MESSENGER");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/file_trans.png"))); // NOI18N
        jButton1.setText("Chose and send file");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(listChat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(32, 32, 32)
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addGap(34, 34, 34))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtInputText, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSend)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(listChat, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton4))
                .addGap(4, 4, 4)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtInputText, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
                    .addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(28, 28, 28))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        if (!this.txtInputText.getText().equals("")) {
            this.listChat.add("Server : " + this.txtInputText.getText());
            for (int i = 0; i < MainSever.listConnection.size(); i++) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                Socket _tempsock = temp.getSOCK();
                if (_tempsock.getPort() == _sockhientai.getPort()) {

                    temp.getConnectionChat().bufferedWriter.println(this.txtInputText.getText());
                    temp.getConnectionChat().bufferedWriter.flush();
                    break;
                }
            }
            this.txtInputText.setText("");
        }
    }//GEN-LAST:event_btnSendActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private java.awt.List listChat;
    private javax.swing.JTextField txtInputText;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        if (!this.txtInputText.getText().equals("")) {
            this.listChat.add("Server : " + this.txtInputText.getText());
            for (int i = 0; i < MainSever.listConnection.size(); i++) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                Socket _tempsock = temp.getSOCK();
                if (_tempsock.getPort() == _sockhientai.getPort()) {

                    temp.getConnectionChat().bufferedWriter.println(this.txtInputText.getText());
                    temp.getConnectionChat().bufferedWriter.flush();
                    break;
                }
            }
            this.txtInputText.setText("");
        }
    }
    public void onRead(ChatMessage message) {
        this.listChat.add("Client " + message.getName() + " : " + message.getMessage());
    }
}
